// -*- coding: utf-8 -*- 
/**
 * Project: LanQiao
 * Creator: yanking
 * Create time: 2022-02-20 14:12
 * IDE: IntelliJ IDEA
 * Introduction:
 */
package Queen.copyQueen;

import java.util.Scanner;

public class copyQueen {
    // copy the resolution of the two N Queen question
    public int n, count = 0;
    public int[][] map;

    public void getSolveQueen() {
        Scanner myScanner = new Scanner(System.in);
        System.out.println("Input the map Size ");
        n = myScanner.nextInt();
        map = new int[n][n];
        System.out.println("I am the copy!");
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                map[i][j] = 1;
            }
        }
        // invoke the function put
        put(0, 2);
        System.out.println(count);
    }

    public void put(int t, int s) {
        if (t == n) {
            if (s == 2) {
                put(0, 3);
            } else {
                count++;
            }
            return;
        }
        for (int i = 0; i < n; i++) {
            if (map[t][i] != 1) {
                continue;
            }
            if (check(t, i, s)) {
                // 当前位置符合放置的皇后类型;
                map[t][i] = s;
            } else {
                continue;
            }
            put(t + 1, s);
            map[t][i] = 1; // 回溯算法需要抹去脚印放置影响下次遍历
        }
        return;
    }

    /**
     * to check the (t,i) if is right to put a chess s
     *
     * @param t  : 当前行
     * @param i  ：当前列
     * @param s： 当前放置的皇后类型
     * @return
     */
    public boolean check(int t, int i, int s) {
        for (int q = t - 1; q >= 0; q--) {
            if (map[q][i] == s) {
                return false;
            }
        }
        for (int q = t - 1, w = i - 1; q >= 0 && w >= 0; q--, w--) {
            if (map[q][w] == s) {
                return false;
            }
        }
        for (int q = t - 1, w = i + 1; q >= 0 && w < n; q--, w++) {
            if (map[q][w] == s) {
                return false;
            }
        }

        return true;
    }

}
